package com.cheng.example.atguigu.design_pattern.principle.ocp.improve;

/*
 * @author 程帅
 * @createTime: 2020/10/18 20:51
 * @Created: with IntelliJ IDEA.
 */
public class Ocp {

    public static void main(String[] args) {
        //使用看着存在的问题
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawShape(new Rectangle());
        graphicEditor.drawShape(new Circle());
        graphicEditor.drawShape(new Triangle());
        //增加新类
        graphicEditor.drawShape(new OtherGraphic());
    }
}

//这是一个用于绘图的类
class GraphicEditor {

    //接收Shape对象，然后根据type,来绘制不同的图形
    public void drawShape(Shape s) {
        s.draw();
    }

}

//Shape类，基类
abstract class Shape {

    int m_type;

    public abstract void draw();//抽象方法
}

//矩形
class Rectangle extends Shape {
    public Rectangle() {
        super.m_type = 1;
    }

    @Override
    public void draw() {
        System.out.println(" 绘制矩形");
    }
}

//圆形
class Circle extends Shape {
    public Circle() {
        super.m_type = 2;
    }

    @Override
    public void draw() {
        System.out.println(" 绘制圆形");
    }
}

//三角形
class Triangle extends Shape {
    public Triangle() {
        super.m_type = 3;
    }

    @Override
    public void draw() {
        System.out.println(" 绘制三角形");
    }
}

class  OtherGraphic extends Shape {
    public OtherGraphic() {
        super.m_type = 4;
    }

    @Override
    public void draw() {
        System.out.println(" 绘制其他图形");
    }
}
